var TimeDateBoxMap = new Object();
var TimeDateBox = function(opt){
	this.target = opt.target?opt.target:'timeDate';
	this.type = opt.type?opt.type:'timeDate',
	this.content = opt.content;
	
	this.focusClass = opt.focusClass?opt.focusClass:'_dateTimeCurInput';
	this.normalClass = opt.normalClass?opt.normalClass:'_dateTimeInput';
	
	var dis = '&nbsp;';
	if(opt.value){
		this.time = this.initTime(opt);
		dis = this.getString();
	}else{
		this.time = new Date();
	}
	
	var con = document.getElementById(this.content);
	con.innerHTML = '<div class="'+this.normalClass+'"><a href="#" id="'+this.target+'-dateTime-a">'+dis+'</a></div>';
	this.input = document.getElementById(this.target +'-dateTime-a');
	this.input.onfocus = this.onfocusInput;
	this.input.onblur = this.onblurInput
	this.input.onclick = this.onclickInput;
	
	this.posLeft = this.input.parentNode.positionLeft; 
	this.posTop = this.input.parentNode.positionTop+32;
	
	TimeDateBoxMap[this.target] = this;
}

TimeDateBox.prototype = {
	focusItemClass:'_dateTimeCurItem',
	normalItemClass:'_dateTimeItem',
	hasInit:false,
	curTarget:'',
	obj:{
		year:{id:'_dateTime_mid_year',max:2050,min:2000,upId:'_dateTime_up_year',downId:'_dateTime_down_year',val:0},
		month:{id:'_dateTime_mid_month',max:12,min:1,upId:'_dateTime_up_month',downId:'_dateTime_down_month',val:0},
		day:{id:'_dateTime_mid_day',max:31,min:1,upId:'_dateTime_up_day',downId:'_dateTime_down_day',val:0},
		hour:{id:'_dateTime_mid_hour',max:23,min:0,upId:'_dateTime_up_hour',downId:'_dateTime_down_hour',val:0},
		minute:{id:'_dateTime_mid_minute',max:59,min:0,upId:'_dateTime_up_minute',downId:'_dateTime_down_minute',val:0}
	},
	selectDiv:'pDateTimeSelect',
	timeDateDiv:'pDateTimeBox',
	dateDiv:'pDateBox',
	timeDiv:'pTimeBox',
	timeDateDivWidth:480,
	timeDivWidth:200,
	dateDivWidth:280,
	
	initTime:function(opt){
		
		if(typeof(opt.value) == 'string'){
			var t = new Date();
			if(this.type == 'timeDate'){		//日期时间型
				var reg = /^(\d{4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2})$/ ;
				if(reg.test(opt.value)){
					var arr = reg.exec(opt.value);
					t.setFullYear(parseInt(arr[1],10),parseInt(arr[2],10),parseInt(arr[3],10));
					t.setHours(parseInt(arr[4],10),parseInt(arr[5],10));
				}
			}else if(this.type =='date'){
				var reg = /^(\d{4})\-(\d{1,2})\-(\d{1,2})$/ ;
				if(reg.test(opt.value)){
					var arr = reg.exec(opt.value);
					t.setFullYear(parseInt(arr[1],10),parseInt(arr[2],10),parseInt(arr[3],10));
				}
			}else if(this.type == 'time'){
				var reg = /^(\d{1,2}):(\d{1,2})$/ ;
				if(reg.test(opt.value)){
					var arr = reg.exec(opt.value);
					t.setHours(parseInt(arr[1],10),parseInt(arr[2],10));
				}
			}
			
			return t;
		}else{
			return opt.value;
		}
	},
	getString:function(){
		var format = '';
		if(this.type == 'timeDate'){
			format='yyyy-MM-dd hh:mm'
		}else if(this.type =='date'){
			format='yyyy-MM-dd';
		}else if(this.type == 'time'){
			format='hh:mm';
		}	
		return this.time.format(format);
	},
	setInputString:function(){
		this.input.innerHTML2 = this.getString();
	},
	changeValue:function(){
		this.time.setYear(this.obj.year.val);
		this.time.setMonth(this.obj.month.val -1);
		this.time.setDate(this.obj.day.val);
		this.time.setHours(this.obj.hour.val);
		this.time.setMinutes(this.obj.minute.val);
		this.setInputString();
	},
	setFocus:function(){
		this.input.focus();
	},
	onfocusInput:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var idArray = id.split('-');
		var _this = TimeDateBoxMap[idArray[0]];
		
		par.className = _this.focusClass;
		
	},
	onblurInput:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var idArray = id.split('-');
		var _this = TimeDateBoxMap[idArray[0]];
		par.className = _this.normalClass;
	},
	onclickInput:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var idArray = id.split('-');
		var _this = TimeDateBoxMap[idArray[0]];
		_this.show();
	},
	show:function(){
		if(!this.hasInit){
			this.init();
			this.hasInit = true;
		}
		this.curTarget = this.target;
		var timeDateDiv = document.getElementById(this.timeDateDiv);
		var a;
		var aList = timeDateDiv.getElementsByTagName('a');
		for(var i =0 ;i<aList.length;i++){
			a = aList[i];
			a.setAttribute('tar',this.target);alert(this.target);
		}
		timeDateDiv.style.left = this.posLeft;
		timeDateDiv.style.top = this.posTop;
		timeDateDiv.style.display = 'block';
		
		var dateDiv = document.getElementById(this.dateDiv);
		var timeDiv = document.getElementById(this.timeDiv);
		
		this.obj.year.val = this.time.getFullYear();
		this.obj.month.val = this.time.getMonth()+1;
		this.obj.day.val = this.time.getDate();
		this.obj.hour.val = this.time.getHours();
		this.obj.minute.val = this.time.getMinutes();
		if(this.type == 'timeDate'){		//日期时间型
			timeDateDiv.style.width = this.timeDateDivWidth;
			this.setTypeValue('year',this.obj.year.val);
			this.setTypeValue('month',this.obj.month.val);
			this.setTypeValue('day',this.obj.day.val);
			this.setTypeValue('hour',this.obj.hour.val );
			this.setTypeValue('minute',this.obj.minute.val);
			
			dateDiv.style.display = 'block';
			timeDiv.style.display = 'block';
			
			document.focus(this.obj.year.id);
		}else if(this.type =='date'){
			timeDateDiv.style.width = this.dateDivWidth;
			dateDiv.style.display = 'block';
			timeDiv.style.display = 'none';
			this.setTypeValue('year',this.obj.year.val);
			this.setTypeValue('month',this.obj.month.val);
			this.setTypeValue('day',this.obj.day.val);
			document.focus(this.obj.year.id);
		}else if(this.type == 'time'){
			timeDateDiv.style.width = this.timeDivWidth;
			dateDiv.style.display = 'none';
			timeDiv.style.display = 'block';
			this.setTypeValue('hour',this.obj.hour.val );
			this.setTypeValue('minute',this.obj.minute.val);
			document.focus(this.obj.hour.id);
		}
		
		
	},
	setTypeValue:function(type,val){
		var a = document.getElementById(this.obj[type].id);
		a.innerHTML2 = val>9?val:'0'+val;
		
		var upVal = val+1; if(upVal > this.obj[type].max){upVal = this.obj[type].min}
		var up = document.getElementById(this.obj[type].upId);	
		up.innerHTML2 = upVal>9?upVal:'0'+upVal;	
		
		var downVal = val-1; if(downVal < this.obj[type].min){downVal = this.obj[type].max}
		var donw = document.getElementById(this.obj[type].downId);	
		donw.innerHTML2 = downVal>9?downVal:'0'+downVal;	
	},
	init:function(){
		var timeDateDiv = document.getElementById(this.timeDateDiv);
		var a;
		var aList = timeDateDiv.getElementsByTagName('a');
		for(var i =0 ;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.focusItem;
			a.onblur = this.blurItem;
			a.onclick = this.clickItem;
		}
	},
	focusItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var tar = a.getAttribute('tar');
		
		var _this = TimeDateBoxMap[tar];
		par.className = _this.focusItemClass;

		var l = par.positionLeft - _this.posLeft +( par.offsetWidth)/2 - 5;
		var selectDiv = document.getElementById(_this.selectDiv);
		selectDiv.style.left = l;
		
		KEY.CURTYPE = 'timeDate';
	},
	blurItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var tar = a.getAttribute('tar');
		
		var _this = TimeDateBoxMap[tar];
		par.className = _this.normalItemClass;
	},
	clickItem:function(event){
		
	},
	hide:function(){
		KEY.CURTYPE = false;
		this.input.focus();
		var timeDateDiv = document.getElementById(this.timeDateDiv);
		timeDateDiv.style.display = 'none';
	},
	down:function(id){
		var idArr = id.split('_');
		var type = idArr[idArr.length - 1];
		var val = this.obj[type].val;
		val--;
		if(val<this.obj[type].min){val = this.obj[type].max;}
		this.obj[type].val = val;
		this.setTypeValue(type,val);
		
		this.changeValue();
	},
	up:function(id){
		var idArr = id.split('_');
		var type = idArr[idArr.length - 1];
		var val = this.obj[type].val;
		val++;
		if(val>this.obj[type].max){val = this.obj[type].min;}
		this.obj[type].val = val;
		this.setTypeValue(type,val);
		this.changeValue();
	}
}

KEY.TYPE['timeDate'] = keyRecvTimeDate;

function keyRecvTimeDate(key){
	var a = document.activeElement;
	var tar = a.getAttribute('tar'); 
	var _this = TimeDateBoxMap[tar];
	if(key == 0x11){		//返回
		_this.hide();
		return false;
	}else if(key ==0xA){		//向上
		_this.up(a.id);
		return false;
	}else if(key == 0xB){		//向下
		_this.down(a.id);
		return false;
	}
	return true;
}
